//
//  ImportTool.swift
//  CrseAsst
//
//  Created by 申铭 on 2024/3/4.
//

import Foundation

class ImportTool {
    /// 导入成语
    static func importIdiom() async -> Array<ImportResult>? {
        log.verbose("导入成语")
        guard let path = Bundle.main.url(forResource: "成语", withExtension: "txt") else {
            log.error("导入成语失败，路径为空")
            return nil
        }
            
        do {
            let data = try Data(contentsOf: path)
            let text = String(data: data, encoding: .utf8) ?? ""
            let strs = text.split(separator: "\n")
            
            var results = [ImportResult]()
            for str in strs {
                results.append(IdiomStore.saveOrUpdate(str: String(str)))
            }
            
            return results
        } catch {
            log.error("导入成语失败，error:\(error)")
        }
        
        return nil
    }
}
